package org.cjlm.ui.template;

import org.cjlm.ui.pub.MButton;
import org.cjlm.ui.pub.MTable;
import org.cjlm.vo.SuperVO;
import org.cjlm.vo.template.TemplateAVO;
import org.cjlm.vo.template.TemplateActionVO;
import org.cjlm.vo.template.TemplateBVO;
import org.cjlm.vo.template.TemplateMVO;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BillListPanel extends JPanel {
    private TemplateMVO mvo;
    private MTable headTable;
    private DefaultTableModel headTableModel;
    private JTabbedPane detailTabs = new JTabbedPane();
    private Map<String, MTable> bodyTableMap = new HashMap<>();
    private Map<String, DefaultTableModel> bodyModelMap = new HashMap<>();
    private final List<MButton> buttons = new ArrayList<>();

    public BillListPanel(TemplateMVO mvo) {
        super();
        this.mvo = mvo;
        initUI();
    }

    private void initUI() {
        setLayout(new BorderLayout(10, 10));
        initHeadTable();
        JScrollPane orderScrollPane = new JScrollPane(headTable);
        orderScrollPane.setPreferredSize(new  Dimension(0, 200));
        initBodyTable();
        // 组装列表界面
        JSplitPane splitPane = new JSplitPane(
                JSplitPane.VERTICAL_SPLIT,
                orderScrollPane,
                detailTabs
        );
        splitPane.setResizeWeight(0.6);  // 60%空间给主表，40%给子表
        splitPane.setOneTouchExpandable(true);  // 添加快速折叠按钮

        add(createBtnPanel(),  BorderLayout.NORTH);
        add(splitPane,  BorderLayout.CENTER); // 将分割窗格放入CENTER
    }

    private void initHeadTable() {
        // 主表
        String[] orderColumns = new String[0];
        if(this.mvo != null){
            List<String> columnlist = new ArrayList<>();
            List<SuperVO> avolist = this.mvo.getChildren();
            for(SuperVO avol : avolist) {
                TemplateAVO avo = (TemplateAVO) avol;
                if (avo.getType() == 1 && avo.getIszb()) {
                    List<SuperVO> bvolist = avo.getChildren(TemplateBVO.class.getName());
                    for(SuperVO bvol : bvolist){
                        TemplateBVO bvo = (TemplateBVO) bvol;
                        columnlist.add(bvo.getName());
                    }
                    break;
                }
            }
            if(columnlist.size() > 0){
                orderColumns = columnlist.toArray(new String[columnlist.size()]);
            }
        }
        headTableModel = new DefaultTableModel(orderColumns, 0){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        headTable = new MTable(headTableModel);
        headTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    private void initBodyTable() {
        // 子表层叠面板
        detailTabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        if(this.mvo != null){
            List<SuperVO> avolist = this.mvo.getChildren();
            for(SuperVO avol : avolist) {
                TemplateAVO avo = (TemplateAVO) avol;
                if (avo.getType() == 1 && !avo.getIszb()) {//子表
                    List<String> columnlist = new ArrayList<>();
                    List<SuperVO> bvolist = avo.getChildren(TemplateBVO.class.getName());
                    if(bvolist != null){
                        for(SuperVO bvol : bvolist){
                            TemplateBVO bvo = (TemplateBVO) bvol;
                            columnlist.add(bvo.getName());
                        }
                    }

                    String[] columns = new String[columnlist.size()]; columnlist.toArray(columns);
                    DefaultTableModel bodyModel = new DefaultTableModel(columns, 0);
                    this.bodyModelMap.put(avo.getCode(), bodyModel);
                    MTable bodyTable = new MTable(bodyModel);
                    this.bodyTableMap.put(avo.getCode(), bodyTable);
                    detailTabs.addTab(avo.getName(), new JScrollPane(bodyTable));
                }
            }
        }
    }

    private JPanel createBtnPanel() {
        if(this.mvo != null){
            List<SuperVO> avolist = this.mvo.getChildren();
            for(SuperVO avol : avolist){
                TemplateAVO avo = (TemplateAVO) avol;
                if(avo.getType() == 1 && avo.getIszb()){
                    List<SuperVO> actionlist = avo.getChildren(TemplateActionVO.class.getName());
                    for(SuperVO actionvo : actionlist){
                        TemplateActionVO action = (TemplateActionVO)actionvo;
                        if(action.getCardlist() == 2 || action.getCardlist() == 3){
                            this.buttons.add(new MButton(action.getCode(), action.getName(), action.getActiontype(), action.getIsedit() == 1 ? true : false));
                        }
                    }
                    break;
                }
            }
        }
        //显示按钮
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
        for(MButton button : this.buttons){
            buttonPanel.add(button);
        }

        return buttonPanel;
    }

    public List<MButton> getButtons() {
        return buttons;
    }
}
